////  ViewController.m
//  UI1_JSON 网易
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 yh. All rights reserved.
//

//<key>NSAppTransportSecurity</key>
//<dict>
//<key>NSAllowsArbitraryLoads</key>
//<true/>
//</dict>


#import "ViewController.h"
#import "NewsTitle.h"
#import "LabelCollectionViewLayout.h"
#import "SelfSizeTool.h"
#import <AVFoundation/AVFoundation.h>
#import "MyCollectionViewCell.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, LabelCollectionViewLayoutDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray<NewsTitle *> *dataArray; // 泛型
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
    LabelCollectionViewLayout *layout = (LabelCollectionViewLayout *)self.myCollectionView.collectionViewLayout;
    layout.delegate = self;

}

- (void)handleData {
    self.dataArray = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://c.m.163.com/nc/topicset/android/subscribe/manage/listspecial.html"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSArray *array = dic[@"tList"];
        for (NSDictionary *dic in array) {
            NewsTitle *news = [NewsTitle newsTitleWithDictionary:dic];
            [self.dataArray addObject:news];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.myCollectionView reloadData];
        });
        NSLog(@"%@", self.dataArray);
    }];

//    NSString *pathJSON = [[NSBundle mainBundle] pathForResource:@"news" ofType:@".JSON"];
//    NSData *jsonData = [NSData dataWithContentsOfFile:pathJSON];
//    NSError *error = nil;
//    NSDictionary *newsDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
//    NSArray *list = newsDic[@"tList"];
//    // 数组一定要初始化
//    self.dataArray = [NSMutableArray array];
//    for (NSDictionary *dic in list) {
//        NewsTitle *news = [NewsTitle newsTitleWithDictionary:dic];
//        [self.dataArray addObject:news];
//    }
//    NSLog(@"%@", self.dataArray);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height widthForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 向上取整  ceilf()
    return ceilf([SelfSizeTool selfSizeWidthWithString:[self.dataArray[indexPath.row] tname]height:height font:[UIFont systemFontOfSize:17]]);
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.titleLabel.text = [self.dataArray[indexPath.row] tname];
    return cell;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
